//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "FdmSimple2dBSSolver.h"
using namespace Cephei::QL::Methods::Finitedifferences::Solvers;
#include <gen/QL/Processes/GeneralizedBlackScholesProcess.h>
#include <gen/QL/Methods/Finitedifferences/Solvers/FdmSolverDesc.h>
#include <gen/QL/Methods/Finitedifferences/Solvers/FdmSchemeDesc.h>
using namespace Cephei::QL::Processes;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSimple2dBSSolver::CFdmSimple2dBSSolver (Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ process, Double strike, Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSolverDesc^ desc, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^>^ schemeDesc) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CGeneralizedBlackScholesProcess^ _Cprocess;
    CFdmSolverDesc^ _Cdesc;
    CFdmSchemeDesc^ _CschemeDesc;
    try
    {
#ifdef HANDLE
        _phFdmSimple2dBSSolver = NULL;
#endif
        _Cprocess = safe_cast<CGeneralizedBlackScholesProcess^> (process);
        _Cprocess->Lock();
        Handle<QuantLib::GeneralizedBlackScholesProcess>& _process = static_cast<Handle<QuantLib::GeneralizedBlackScholesProcess>&> (_Cprocess->GetHandle ()); 
        QuantLib::Real _strike = (QuantLib::Real)ValueHelper::Convert (strike); //d
        _Cdesc = safe_cast<CFdmSolverDesc^> (desc);
        _Cdesc->Lock();
        QuantLib::FdmSolverDesc& _desc = static_cast<QuantLib::FdmSolverDesc&> (_Cdesc->GetReference ()); 
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^>::IsSome::get (schemeDesc))
        {
            _CschemeDesc = safe_cast<CFdmSchemeDesc^> (schemeDesc->Value);
            _CschemeDesc->Lock();
        }
        QuantLib::FdmSchemeDesc& _schemeDesc = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^>::IsSome::get (schemeDesc) ? static_cast<QuantLib::FdmSchemeDesc&> (_CschemeDesc->GetReference ()) : QuantLib::FdmSchemeDesc::Douglas()); //1
        _ppFdmSimple2dBSSolver = new boost::shared_ptr<QuantLib::FdmSimple2dBSSolver> (new QuantLib::FdmSimple2dBSSolver ( _process,  _strike,  _desc,  _schemeDesc ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprocess != nullptr) _Cprocess->Unlock();
        if (_Cdesc != nullptr) _Cdesc->Unlock();
        if (_CschemeDesc != nullptr) _CschemeDesc->Unlock();
    }
}
Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSimple2dBSSolver::CFdmSimple2dBSSolver (boost::shared_ptr<QuantLib::FdmSimple2dBSSolver>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmSimple2dBSSolver = NULL;
#endif
	_ppFdmSimple2dBSSolver = &childNative;
    
}
Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSimple2dBSSolver::CFdmSimple2dBSSolver (QuantLib::FdmSimple2dBSSolver& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phFdmSimple2dBSSolver = NULL;
#endif
	_ppFdmSimple2dBSSolver = new boost::shared_ptr<QuantLib::FdmSimple2dBSSolver> (&childNative);
    
    _FdmSimple2dBSSolverOwner = owner;
    
}

Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSimple2dBSSolver::CFdmSimple2dBSSolver (CFdmSimple2dBSSolver^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmSimple2dBSSolver = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFdmSimple2dBSSolver = new boost::shared_ptr<QuantLib::FdmSimple2dBSSolver> (copy->GetShared());
        
    }
}
Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSimple2dBSSolver::CFdmSimple2dBSSolver (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmSimple2dBSSolver = NULL;
#endif
	if (!t->IsSubclassOf(CFdmSimple2dBSSolver::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSimple2dBSSolver::CFdmSimple2dBSSolver (QuantLib::Handle<QuantLib::FdmSimple2dBSSolver>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmSimple2dBSSolver = &childNative;
	_ppFdmSimple2dBSSolver = &static_cast<boost::shared_ptr<QuantLib::FdmSimple2dBSSolver>>(childNative.currentLink());
    
    _FdmSimple2dBSSolverOwner = owner;
}
Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSimple2dBSSolver::CFdmSimple2dBSSolver (QuantLib::Handle<QuantLib::FdmSimple2dBSSolver> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmSimple2dBSSolver = &childNative;
	_ppFdmSimple2dBSSolver = &static_cast<boost::shared_ptr<QuantLib::FdmSimple2dBSSolver>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSimple2dBSSolver::CFdmSimple2dBSSolver (QuantLib::FdmSimple2dBSSolver childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmSimple2dBSSolver = NULL;
#endif
	_ppFdmSimple2dBSSolver = new boost::shared_ptr<QuantLib::FdmSimple2dBSSolver> (new QuantLib::FdmSimple2dBSSolver (childNative));
    
}
#endif

Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSimple2dBSSolver::~CFdmSimple2dBSSolver ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppFdmSimple2dBSSolver != NULL)
    {
	    delete _ppFdmSimple2dBSSolver;
        _ppFdmSimple2dBSSolver = NULL;
    }
}
Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSimple2dBSSolver::!CFdmSimple2dBSSolver ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppFdmSimple2dBSSolver != NULL)
    {
	    delete _ppFdmSimple2dBSSolver;
    }
}
QuantLib::FdmSimple2dBSSolver& Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSimple2dBSSolver::GetReference ()
{
    if (_ppFdmSimple2dBSSolver == NULL) throw REFNEW NativeNullException ();
	return **_ppFdmSimple2dBSSolver;
}
boost::shared_ptr<QuantLib::FdmSimple2dBSSolver>& Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSimple2dBSSolver::GetShared ()
{
    if (_ppFdmSimple2dBSSolver == NULL) throw REFNEW NativeNullException ();
	return *_ppFdmSimple2dBSSolver;
}
QuantLib::FdmSimple2dBSSolver* Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSimple2dBSSolver::GetPointer ()
{
    if (_ppFdmSimple2dBSSolver == NULL) throw REFNEW NativeNullException ();
	return &**_ppFdmSimple2dBSSolver;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FdmSimple2dBSSolver>& Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSimple2dBSSolver::GetHandle ()
{
	if (_phFdmSimple2dBSSolver == NULL)
	{
		_phFdmSimple2dBSSolver = new Handle<QuantLib::FdmSimple2dBSSolver> (*_ppFdmSimple2dBSSolver);
	}
	return *_phFdmSimple2dBSSolver;
}
#endif
bool Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSimple2dBSSolver::HasNative () 
{
	return (_ppFdmSimple2dBSSolver != NULL);
}

Double Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSimple2dBSSolver::DeltaAt (Double s, Double a, Double eps)
{
    try
    {
        QuantLib::Real _s = (QuantLib::Real)ValueHelper::Convert (s); //a
        QuantLib::Real _a = (QuantLib::Real)ValueHelper::Convert (a); //a
        QuantLib::Real _eps = (QuantLib::Real)ValueHelper::Convert (eps); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFdmSimple2dBSSolver)->deltaAt ( _s,  _a,  _eps );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSimple2dBSSolver::GammaAt (Double s, Double a, Double eps)
{
    try
    {
        QuantLib::Real _s = (QuantLib::Real)ValueHelper::Convert (s); //a
        QuantLib::Real _a = (QuantLib::Real)ValueHelper::Convert (a); //a
        QuantLib::Real _eps = (QuantLib::Real)ValueHelper::Convert (eps); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFdmSimple2dBSSolver)->gammaAt ( _s,  _a,  _eps );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSimple2dBSSolver::ThetaAt (Double s, Double a)
{
    try
    {
        QuantLib::Real _s = (QuantLib::Real)ValueHelper::Convert (s); //a
        QuantLib::Real _a = (QuantLib::Real)ValueHelper::Convert (a); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFdmSimple2dBSSolver)->thetaAt ( _s,  _a );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSimple2dBSSolver::ValueAt (Double s, Double a)
{
    try
    {
        QuantLib::Real _s = (QuantLib::Real)ValueHelper::Convert (s); //a
        QuantLib::Real _a = (QuantLib::Real)ValueHelper::Convert (a); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFdmSimple2dBSSolver)->valueAt ( _s,  _a );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSimple2dBSSolver^ Cephei::QL::Methods::Finitedifferences::Solvers::CFdmSimple2dBSSolver_Factory::Create (Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ process, Double strike, Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSolverDesc^ desc, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^>^ schemeDesc)
{
    return REFNEW CFdmSimple2dBSSolver ( process,  strike,  desc,  schemeDesc);
}
